package com.tebiecloud.ali.server.controller;

import com.tebiecloud.ali.client.constants.ResultEnum;
import com.tebiecloud.ali.server.service.AliOssService;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 阿里云oss服务
 */
@RestController
@RequestMapping("/alioss")
public class AliOssController {

    @Autowired
    private AliOssService aliOssService;

    /**
     * 文件上传
     * @param type 图片类型：例如shop(门店图片) , bank(银行卡图片) ,user(店员头像) ,other(其它)
     * @param image
     * @return
     */
    @PostMapping("upload")
    public ResultBody uploadImage(@RequestParam("type") String type, @RequestParam("image") MultipartFile image){
        String data = "";
        try {
            String filename = image.getOriginalFilename();
            String path = "tebie/" + type + "/image/" + DateUtils.formatCurrentDate("yyyyMMdd") + "/";
            String ossKey = path + UUIDUtil.get32UUID() + "." + filename.substring(filename.lastIndexOf(".") + 1);
            data = aliOssService.upload(ossKey, image.getInputStream());
        } catch (Exception e) {
            return ResultBodyUtils.failed(ResultEnum.ALI_IMAGE_UPLOAD_FAILED.getMessage());
        }

        return ResultBodyUtils.ok(data);
    }

    /**
     * 上传二进制文件
     * @param type
     * @param imageBase
     * @return
     */
    @PostMapping("uploadBase64")
    public ResultBody uploadBlob(@RequestParam("type") String type,@RequestParam("imageBase") String imageBase ){
        String data = "";
        imageBase=imageBase.replaceAll("data:image/png;base64,","");
        BASE64Decoder decoder =  new BASE64Decoder();
        byte[] imageByte = null;
        try{
            imageByte = decoder.decodeBuffer(imageBase);
            for (int i = 0; i < imageByte.length; ++i) {
                if (imageByte[i] < 0) {// 调整异常数据
                    imageByte[i] += 256;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        InputStream inputStream = new ByteArrayInputStream(imageByte);
        try {
            String path = "tebie/" + type + "/image/" + DateUtils.formatCurrentDate("yyyyMMdd") + "/";
            String ossKey = path + UUIDUtil.get32UUID() + ".jpg";
            data = aliOssService.upload(ossKey, inputStream);
        } catch (Exception e) {
            return ResultBodyUtils.failed(ResultEnum.ALI_IMAGE_UPLOAD_FAILED.getMessage());
        }
        return ResultBodyUtils.ok(data);
    }
}
